app.controller('GetElequanlistCtrl', function($scope,$http,$state,$location,$localStorage,$window,$interval) {
	$scope.namedata = $localStorage.cur_user.uname;
	$scope.mmData = $localStorage.layers_info;
	$scope.b = {};	
	var initdata = [];
	for(var key in $scope.mmData){
		if(key=="所有学校"){
			initdata[0]="所有学校"
		}
		initdata.push(key);
	}	
	$scope.b.school_name = initdata[0];	
	$scope.getlist = function(){		
		var school_id,building_type,campus_building_id,building_name,layer_id,layer_name;
		if($scope.b.school_name!="所有学校"){			
			if($scope.b.buildingtype!=null&&$scope.b.buildingtype!=undefined&&$scope.b.buildingtype!=""){
				if($scope.b.building_name!=null&&$scope.b.building_name!=undefined&&$scope.b.building_name!=""){
					if($scope.b.layer_name!=null&&$scope.b.layer_name!=undefined&&$scope.b.layer_name!=""){
						school_id = $scope.mmData[$scope.b.school_name].school_id;
						building_type = $scope.b.buildingtype.split('+')[0];
						campus_building_id = $scope.b.layer_name.split('+')[1];
						building_name = $scope.b.building_name;
						layer_name = $scope.b.layer_name.split('+')[0];
						layer_id = $scope.b.layer_name.split('+')[2];						
					}else{
						school_id = $scope.mmData[$scope.b.school_name].school_id;
						building_type = $scope.b.buildingtype.split('+')[0];
						campus_building_id = $scope.mmData[$scope.b.school_name][$scope.b.buildingtype.split('+')[0]][$scope.b.building_name].campus_id;
						building_name = $scope.b.building_name;
						$scope.b.layer_name = "";
						layer_name = "所有层";
					}


				}else{

					school_id = $scope.mmData[$scope.b.school_name].school_id;
					building_type = $scope.b.buildingtype.split('+')[0];
					$scope.b.building_name = "";
					building_name = "所有楼";
				}

			}else{
				$scope.b.building_type = "";
				building_type = "所有类型";
				school_id = $scope.mmData[$scope.b.school_name].school_id;
			}
			
			
		}else{
			school_id = "";
			building_type = "";
			campus_building_id = "";
			building_name = "";
			layer_id = "" ;
			layer_name = "" ;
		}
	     $http.post(
	            $scope.app.backendurl+"tbl-electricity/view-all",
	            {    
	                'page':$scope.currentPage,  
	                'school_name':$scope.b.school_name,
	                'school_id':school_id,	                             	
	                'campus_building_id':campus_building_id,
	                'building_name':building_name,
	                'building_type':building_type,
	                'layer_id':layer_id,
	                'layer_name':layer_name,
	                'token':$localStorage.token                  

	            }
	          ).success(function (data) {
	            //console.log(data);
	            if(data.code==0&&data.msg=="读取成功"){             
	              $scope.aaData = data.data.list;
	              $scope.totalItems = data.data.page.totalCount;          
	            }
	            if(data.code==20001&&data.msg=='请先登录'){
	              $location.path('access.signin');
	            }  

	        $scope.pageChanged = function(){
	        	//console.log($scope.currentPage)
	        	 $http.post(
		            $scope.app.backendurl+"tbl-electricity/view-all",
		            {    
	                'page':$scope.currentPage,  
	                'school_name':$scope.b.school_name,
	                'school_id':school_id,
	                'key':$scope.b.key,	               	
	                'campus_building_id':campus_building_id,
	                'building_name':building_name,
	                'building_type':building_type,
	                'layer_id':layer_id,
	                'layer_name':layer_name,
	                'token':$localStorage.token              

		            }
		          ).success(function (data) {
		          	//console.log(data);
		          	if(data.code==0&&data.msg=="读取成功"){             
		              $scope.aaData = data.data.list;	                    
		            }
		            if(data.code==20001&&data.msg=='请先登录'){
		              $location.path('access.signin');
		            }  
		          }).error(function(data){

	        	 })
	        }    
	        }).error(function(data){
	          console.log(data);
	        })
		}	
	$scope.getlist();
    var timer = $interval($scope.getlist,30000);
    $scope.$on("$destroy", function() {        
	    $interval.cancel(timer);	 
	});

	
});